#ifndef SDL_TEXT_CONTROL_H
#define  SDL_TEXT_CONTROL_H

#include "SDL.h"
#include "sdltext.hpp"
#include "sdlevent.hpp"
#include "sdlcmn.hpp"
#include <iostream>
#include <string>

using namespace std;

class SDLTextControl : public sdlEvent{

private: 
    SDL_Surface *theScreen;
    string theLable;
    sdlRect theRect;
    bool active;
public:
    SDLTextControl(void *parent=NULL):sdlEvent(parent){
        theScreen=NULL;
        theLabel="";
        theRect.x=-1;
        theRect.y=-1;
        theRect.w=0;
        theRect.h=0;
    }
    
    SDLTextControl(void *parent,SDL_surface *parentSurface, const SDL_Rect &aRect,
               const string &label=""):sdlEvent(parent){
        theScreen=parentSurface;
        theLabel=label;
        theRect=aRect;
    }

    void update(SDL_Event &e){
        switch(e.type){
            case SDL_KEYDOWN:
                theLabel+=e.key.keysym.sym);
                break;
            case SDL_MOUSEBUTTONUP:
                sdlRect rect;
                rect.x=e.motion.x;
                rect.y=e.motion.y;
                rect.w=1;
                rect.h=1;
                if(theRect.isCollision(rect){
                    active=true;
                }else{
                    active=false;
                }
                break;   
        }
        draw();
    }

    void clear(){
        SDL_FillRect(theScreen,&theRect,SDL_MapRGB(theScreen->format,3,100,180));
    }
 
    void draw(){
        clear();
        sdlTextEngine::getInstance().renderTectToSurface(theScreen,theRect,theLabel.c_str());
        SDL_UpdateRect(theScreen,theRect.x,theRect.y ,theRect.w,theRect.h);
    }

    void isActive(){
       return active;
    } 
};

#endif                


}
